// Package avatar -----------------------------
// @file      : avatarRoomPmtModule.go
// @author    : WuZhouHai
// @contact   : wzh200x@126.com
// @time      : 2024/7/24 17:08
// @desc      :
// -------------------------------------------
package avatar

import (
	"github.com/gogf/gf/v2/container/garray"
	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/errors/gcode"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/util/gconv"
	"phoenix/apps/examples/phoenix_app_dwys/defines"
	"phoenix/apps/examples/phoenix_app_dwys/defines/entityType"
	"phoenix/apps/examples/phoenix_app_dwys/model/entityModule/avatar"
	"phoenix/apps/examples/phoenix_app_dwys/model/gameConfig"
	"phoenix/phoenix/defines/dataType"
	"phoenix/phoenix/entitys/entityModule"
	"phoenix/phoenix/interfaces"
	"phoenix/phoenix/managers/logicConfigManager"
)

type AvatarRoomPmtModule struct {
	entityModule.EntityModuleBase
	RoomPmtInfo *gmap.IntAnyMap
}

func (this *AvatarRoomPmtModule) Init() {
	this.EntityModuleBase.Init()
	this.SyncRoomPmtInfo()
}

func (this *AvatarRoomPmtModule) SyncRoomPmtInfo() {
	this.RoomPmtInfo.Clear()
	datas := this.Model.GetModelDatas()
	datas.Iterator(func(key int, value interface{}) bool {
		if data, ok := value.(*avatar.AvatarRoomPmtModelData); ok {
			if !this.RoomPmtInfo.Contains(int(data.RoomId)) {
				this.RoomPmtInfo.Set(int(data.RoomId), gmap.NewIntIntMap())
			}
			this.RoomPmtInfo.Get(int(data.RoomId)).(*gmap.IntIntMap).Set(int(data.PmtId), data.Dbid)
		}
		return true
	})
}

func (this *AvatarRoomPmtModule) GetRoomPmtUpLevelConfig(roomId int32) interfaces.ILogicConfig {
	configName := "t_RoomPmtUpLevelConfig_" + gconv.String(roomId)
	return logicConfigManager.PhoenixManagerLogicConfig().GetConfig(configName)
}

func (this *AvatarRoomPmtModule) GetRoomPmtConfig(roomId int32) interfaces.ILogicConfig {
	configName := "t_RoomPmtConfig_" + gconv.String(roomId)
	return logicConfigManager.PhoenixManagerLogicConfig().GetConfig(configName)
}

func (this *AvatarRoomPmtModule) GetRoomPmtTypeConfig(roomId int32) interfaces.ILogicConfig {
	configName := "t_RoomPmtTypeConfig_" + gconv.String(roomId)
	return logicConfigManager.PhoenixManagerLogicConfig().GetConfig(configName)
}

func (this *AvatarRoomPmtModule) GetUpLevelCost(roomId int32, pmtId int32) (int32, error) {
	if this.RoomPmtInfo.Contains(int(roomId)) {
		if this.RoomPmtInfo.Get(int(roomId)).(*gmap.IntIntMap).Contains(int(pmtId)) {
			dbid := this.RoomPmtInfo.Get(int(roomId)).(*gmap.IntIntMap).Get(int(pmtId))
			data := this.Model.GetModelData(dbid).(*avatar.AvatarRoomPmtModelData)
			config := this.GetRoomPmtUpLevelConfig(roomId)
			if config == nil {
				return -1, gerror.NewCodef(gcode.New(defines.CUSTOMER_ERROR_CONFIG_NOT_EXIST, "", nil), "配置不存在")
			}
			configKey := data.PmtId*10000 + data.Level*100 + data.PmtLevel
			itemConfigData := config.GetConfigItem(configKey)
			if itemConfigData == nil {
				return -1, gerror.NewCodef(gcode.New(defines.CUSTOMER_ERROR_ROOM_PMT_LEVEL, "", nil), "等级配置不存在"+gconv.String(configKey))
			}
			itemConfig := itemConfigData.(*gameConfig.RoomPmtUpLevel)
			return itemConfig.GoldCost, nil
		} else {
			return -1, gerror.NewCodef(gcode.New(defines.CUSTOMER_ERROR_ROOM_PMT_NOT_EXIST, "", nil), "摆件未激活")
		}
	} else {
		return -1, gerror.NewCodef(gcode.New(defines.CUSTOMER_ERROR_ROOM_NOT_EXIST, "", nil), "店铺未激活")
	}
}

func (this *AvatarRoomPmtModule) UpCyLevel(roomId int32) error {
	if this.RoomPmtInfo.Contains(int(roomId)) {
		pmtInfo := this.RoomPmtInfo.Get(int(roomId)).(*gmap.IntIntMap)
		result := true
		pmtInfo.Iterator(func(key int, value int) bool {
			data := this.Model.GetModelData(value).(*avatar.AvatarRoomPmtModelData)
			if data.PmtLevel != data.Level*10 {
				result = false
			}
			return true
		})
		if result {
			pmts := this.RoomPmtInfo.Get(int(roomId)).(*gmap.IntIntMap)
			pmts.Iterator(func(key int, value int) bool {
				data := this.Model.GetModelData(value).(*avatar.AvatarRoomPmtModelData)
				if data.Level >= 7 {
					return false
				}
				data.Level += 1
				//data.PmtLevel = (data.Level - 1) * 10
				this.Model.SetModelDataUpdate(value)
				return true
			})
		} else {
			return gerror.NewCodef(gcode.New(defines.CUSTOMER_ERROR_ROOM_PMT_LEVEL, "", nil), "等级不满足")
		}
	} else {
		return gerror.NewCodef(gcode.New(defines.CUSTOMER_ERROR_ROOM_NOT_EXIST, "", nil), "店铺未激活")
	}
	return nil
}

func (this *AvatarRoomPmtModule) CheckCanUpLevelPmt(roomId int32, pmtId int32) error {
	if this.RoomPmtInfo.Contains(int(roomId)) {
		if this.RoomPmtInfo.Get(int(roomId)).(*gmap.IntIntMap).Contains(int(pmtId)) {
			dbid := this.RoomPmtInfo.Get(int(roomId)).(*gmap.IntIntMap).Get(int(pmtId))
			data := this.Model.GetModelData(dbid).(*avatar.AvatarRoomPmtModelData)
			if data.PmtLevel == data.Level*10 {
				return gerror.NewCodef(gcode.New(defines.CUSTOMER_ERROR_ROOM_PMT_LEVEL_MAX, "", nil), "等级已满,请先升阶")
			}
		} else {
			return gerror.NewCodef(gcode.New(defines.CUSTOMER_ERROR_ROOM_PMT_NOT_EXIST, "", nil), "摆件未激活")
		}
	} else {
		return gerror.NewCodef(gcode.New(defines.CUSTOMER_ERROR_ROOM_NOT_EXIST, "", nil), "店铺未激活")
	}
	return nil
}

func (this *AvatarRoomPmtModule) UplevelPmt(roomId int32, pmtId int32, isCheck bool) error {
	if isCheck {
		err := this.CheckCanUpLevelPmt(roomId, pmtId)
		if err != nil {
			return err
		}
	}
	dbid := this.RoomPmtInfo.Get(int(roomId)).(*gmap.IntIntMap).Get(int(pmtId))
	data := this.Model.GetModelData(dbid).(*avatar.AvatarRoomPmtModelData)
	data.PmtLevel += 1
	this.Model.SetModelDataUpdate(dbid)
	return nil
}

func (this *AvatarRoomPmtModule) GetPmtInfo(roomId int32, pmtId int32) *avatar.AvatarRoomPmtModelData {
	if this.RoomPmtInfo.Contains(int(roomId)) {
		if this.RoomPmtInfo.Get(int(roomId)).(*gmap.IntIntMap).Contains(int(pmtId)) {
			dbid := this.RoomPmtInfo.Get(int(roomId)).(*gmap.IntIntMap).Get(int(pmtId))
			return this.Model.GetModelData(dbid).(*avatar.AvatarRoomPmtModelData)
		} else {
			return nil
		}
	}
	return nil
}

func (this *AvatarRoomPmtModule) GetRoomPmtInfo(roomId int32) *garray.Array {
	pmts := garray.NewArray()
	if this.RoomPmtInfo.Contains(int(roomId)) {
		this.RoomPmtInfo.Get(int(roomId)).(*gmap.IntIntMap).Iterator(func(key int, value int) bool {
			data := this.Model.GetModelData(value)
			pmts.Append(data)
			return true
		})
	}
	return pmts
}

func (this *AvatarRoomPmtModule) ActiveRoom(roomId int32) error {
	err := this.Model.(*avatar.AvatarRoomPmtModel).CreatePmt(roomId)
	if err != nil {
		return err
	}
	this.SyncRoomPmtInfo()
	return nil
}

func (this *AvatarRoomPmtModule) CheckCanActiveRoom(roomId int32) (bool, error) {
	if roomId <= 1 {
		return false, nil
	}
	if this.RoomPmtInfo.Contains(int(roomId)) {
		return false, gerror.NewCodef(gcode.New(defines.CUSTOMER_ERROR_ROOM_ACTIVED, "", nil), "店铺已激活")
	}
	preRoomId := roomId - 1
	result := true
	if this.RoomPmtInfo.Contains(int(preRoomId)) {
		this.RoomPmtInfo.Get(int(preRoomId)).(*gmap.IntIntMap).Iterator(func(key int, value int) bool {
			data := this.Model.GetModelData(value).(*avatar.AvatarRoomPmtModelData)
			if data.Level != 3 {
				result = false
			}
			return false
		})
	}
	if !result {
		return false, gerror.NewCodef(gcode.New(defines.CUSTOMER_ERROR_ROOM_CYLEVLE, "", nil), "前置店铺阶级需要达到3级")
	}
	return true, nil
}

func NewAvatarRoomPmtModule(entityDBID dataType.PhoenixTypeDataBaseId) interfaces.IEntityModule {
	return &AvatarRoomPmtModule{
		EntityModuleBase: entityModule.NewEntityModuleBase(entityType.EntityName_Avatar,
			defines.ModuleName_Avatar_RoomPmt, entityDBID),
		RoomPmtInfo: gmap.NewIntAnyMap(true),
	}
}
